package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.BaseException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.BsKefuCenter;
import com.ruoyi.system.mapper.BsKefuCenterMapper;
import com.ruoyi.system.service.BsKefuCenterService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.List;

/**
 * @Description TODO
 * @ClassName BsKefuCenterServiceImpl
 * @Author kenn
 * @Version 1.0.0
 * @Date 2023年10月20日 09:43:00
 */
@Service
public class BsKefuCenterServiceImpl extends ServiceImpl<BsKefuCenterMapper, BsKefuCenter> implements BsKefuCenterService {

    @Override
    public TableDataInfo<BsKefuCenter> pageList(PageDomain pageDomain, String question, String type) {
        LambdaQueryWrapper<BsKefuCenter> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(question), BsKefuCenter::getQuestion, question);
        queryWrapper.eq(StringUtils.isNotEmpty(type), BsKefuCenter::getQuestionType, type);
        queryWrapper.orderByDesc(BsKefuCenter::getAccessCount);
        PageHelper.startPage(pageDomain.getPageNum(), pageDomain.getPageSize());
        List<BsKefuCenter> zhusuList = list(queryWrapper);
        PageInfo<BsKefuCenter> pageInfo = new PageInfo<>(zhusuList);
        return new TableDataInfo<>(pageInfo.getList(), pageInfo.getTotal());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveEntity(BsKefuCenter kefuCenter) {
        LambdaQueryWrapper<BsKefuCenter> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BsKefuCenter::getQuestion, kefuCenter.getQuestion());
        queryWrapper.eq(BsKefuCenter::getQuestionType, kefuCenter.getQuestionType());
        int sameNameCount = count(queryWrapper);
        if (sameNameCount > 0) {
            throw new BaseException("问题已存在");
        }
        kefuCenter.setAccessCount(0L);
        return save(kefuCenter);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateEntity(BsKefuCenter kefuCenter) {
        LambdaQueryWrapper<BsKefuCenter> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ne(BsKefuCenter::getId, kefuCenter.getId());
        queryWrapper.eq(BsKefuCenter::getQuestion, kefuCenter.getQuestion());
        queryWrapper.eq(BsKefuCenter::getQuestionType, kefuCenter.getQuestionType());
        int sameNameCount = count(queryWrapper);
        if (sameNameCount > 0) {
            throw new BaseException("问题已存在");
        }
        return updateById(kefuCenter);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        Assert.notNull(id, "主键id不能为空");
        return removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public BsKefuCenter click(Long id) {
        Assert.notNull(id, "主键id不能为空");
        BsKefuCenter kefuCenter = getById(id);
        Assert.notNull(kefuCenter, "获取问题失败, 请刷新后重试");
        Long originalCount = kefuCenter.getAccessCount();
        kefuCenter.setAccessCount(++originalCount);
        updateById(kefuCenter);
        return kefuCenter;
    }

}
